package com.probiz.estore.content.dao.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.probiz.estore.Constants;
import com.probiz.estore.common.model.content.Newsletter;
import com.probiz.estore.content.dao.NewsletterDao;
import com.probiz.estore.core.dao.impl.HibernateGenericDaoImpl;
import com.probiz.estore.core.model.BaseObject;
import com.probiz.estore.core.search.SearchCriteria;

/**
 * Dao implementation for Newsletter.
*/
public class NewsletterDaoImpl extends HibernateGenericDaoImpl<Newsletter> implements NewsletterDao {
	
	@Override
	protected void initFilterHqlMap() {
		filterHqlMap.put(BaseObject.FilterHqlName.DEFAULT, "select s from Newsletter s");
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.content.dao.NewsletterDao#getAllNewslettersToSend(java.lang.Integer, int, int)
	 */
	public List<Newsletter> getAllNewslettersToSend(Integer storeId, int pageNo, int pageSize){
		
		Calendar cal=Calendar.getInstance();
		cal.set(cal.HOUR_OF_DAY, 0);
		cal.set(cal.MINUTE, 0);
		cal.set(cal.SECOND, 0);
		Date dt1=cal.getTime();
		
		cal.add(cal.HOUR_OF_DAY, 23);
		cal.add(cal.MINUTE, 59);
		cal.add(cal.SECOND, 59);
		Date dt2 = cal.getTime();
		
		String hql = "from Newsletter nl where (nl.status=? or nl.status is null) and nl.sendTime>=? and nl.sendTime<=?";
		SearchCriteria sc = SearchCriteria.getHqlPagingInstance(hql, new Object[]{Constants.STATUS_NOT_SENT, dt1, dt2},
				pageNo, pageSize, null);
		List<Newsletter> list=this.searchByCriteria(sc);
		for(Newsletter newsletter: list){
			this.initialize(newsletter.getNewsletterGroups());
		}
		return list;
	}

}
